package com.squapsource.project_euler;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.squapsource.project_euler.utils.Divisors;

public class Problem21 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Map<Integer, Integer> divisors = new HashMap<Integer, Integer>();
		int sumAmicable = 0;
		for (int i = 1; i < 10000; i++) {
			Set<Integer> divs = Divisors.getNumDivisors(i);
			divs.remove(i);
			int sumDivs = 0;
			for (Integer div : divs)
				sumDivs += div;
			divisors.put(i, sumDivs);
		}
		for (int i = 1; i < 10000; i++) {
			if (divisors.get(divisors.get(i)) != null && i != divisors.get(i)
					&& divisors.get(divisors.get(i)).intValue() == i)
				sumAmicable += i;
		}

		System.out.println(sumAmicable);

	}

}
